package com.lee.rabbitmq.exchange.topic;

import com.lee.rabbitmq.util.RabbitMqUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * 使用topic策略交换机接收消息
 *
 * @author Lee
 * @createDate 2021/12/15 10:52
 */
public class Receive3 {
    public static void main(String[] args) {
        Connection connection = RabbitMqUtil.getConnection();
        Channel channel = null;
        try {
            channel = connection.createChannel();


            channel.queueDeclare("topicQueue03",true,false,false,null);
            //声明交换机
            channel.exchangeDeclare("topicExchange","topic",true);
            //将这个随机的队列绑定到交换机中，由于是fanout类型的交换机因此不需指定RoutingKey进行绑定
            channel.queueBind("topicQueue03","topicExchange","aa.#");

            /**
             * 监听某个队列并获取队列中的数据
             * 注意:
             *  当前被绑定的队列必须已经存在并正确地绑定到了某个交换机中
             */
            channel.basicConsume("topicQueue03",true,new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    String message = new String(body);
                    System.out.println("消费者aa.#: "+message);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
